package com.xusheng.cli.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author jzw
 * @since 2022-08-18 14:43
 */
@Configuration
public class UploadConfig {

	@Bean
	public MultipartConfigElement multipartConfigElement(@Value("${spring.servlet.multipart.location}") String tmp) throws IOException {
		MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
		Path directoryPath = Paths.get(tmp);
		if (Files.notExists(directoryPath, LinkOption.NOFOLLOW_LINKS)) {
			Files.createDirectories(directoryPath);
		}
		multipartConfigFactory.setLocation(tmp);
		return multipartConfigFactory.createMultipartConfig();
	}

}
